package com.gc.web.controller.api;

import com.gc.business.domain.VersionControl;
import com.gc.business.service.IVersionControlService;
import com.gc.common.config.Global;
import com.gc.common.constant.Constants;
import com.gc.common.utils.StringUtils;
import com.gc.framework.util.FileUploadUtils;
import com.gc.framework.web.base.ApiBaseController;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hruiyan
 * @title: banner api接口层
 * @date 2020年12月24日
 */
@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/api/version")
public class ApiVersionController extends ApiBaseController {

    protected final Logger logger = LoggerFactory.getLogger(ApiVersionController.class);
    @Autowired
    private IVersionControlService versionControlService;

    /**
     * 查询最新安装包
     *
     * @param version 00.00
     */
    @PostMapping("/info")
    @ResponseBody
    public Map<String, Object> info(String version) {
        if (StringUtils.isEmpty(version)) {
            return error(Constants.UNVERIFY, "参数有误:版本号不存在");
        }
        Map<String, Object> map = new HashMap<>();
        String[] currentVersionSplit = version.split("\\.");
        VersionControl resultVersion = versionControlService.selectNewVersionControl();
        if (resultVersion == null) {
            return error(Constants.UNVERIFY, "未查到相关应用包");
        }
        String[] resultVersionsSplit = resultVersion.getVersionNumber().split("\\.");
        Integer currentVersionFirst = Integer.valueOf(currentVersionSplit[0]);
        Integer resultVersionsFirst = Integer.valueOf(resultVersionsSplit[0]);
        Integer currentVersionLast = Integer.valueOf(currentVersionSplit[1] + currentVersionSplit[2]);
        Integer resultVersionsLast = Integer.valueOf(resultVersionsSplit[1] + resultVersionsSplit[2]);
        //先判断是否热更新
        if (currentVersionFirst.intValue() == resultVersionsFirst.intValue()) {
            if (currentVersionLast.intValue() < resultVersionsLast.intValue()) {
                //热部署更新
                map.put("update",true);
                map.put("wgtUpdate", true);
                map.put("wgtUrl", resultVersion.getWgtUrl());
                map.put("Android", "");
                map.put("iOS", "");
                return success(map);
            } else {
                //不更新
                map.put("update", false);
                map.put("wgtUpdate", false);
                map.put("wgtUrl", "");
                map.put("Android", "");
                map.put("iOS", "");
                return success(map);
            }
        } else {
            //整包更新
            map.put("update", true);
            map.put("wgtUpdate", false);
            map.put("wgtUrl", "");
            map.put("Android", resultVersion.getApkUrl());
            map.put("iOS", resultVersion.getIpaUrl());
            return success(map);
        }
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, Object> uploadFile(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String url = "";
        System.out.print("图片                     " + file);
        try {
            if (!file.isEmpty()) {
                url = FileUploadUtils.upload(Global.getUploadPath(), file);
            }
        } catch (Exception e) {
            log.info("===============" + e);
            return error(Constants.UNVERIFY, e.getMessage());
        }
        map.put("headPortrait", url);
        log.info(map.toString());
        return success(map);
    }

}
